<html>
<head>
<script src="../../resources/js-test.js"></script>
<script>
'use strict';
var elem, select;
var ns = "http://not-html.test";

function testDocumentProperty(attributeName, elementName, base) {
  var elem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElementNS(ns, elementName);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + "");
  document.body.removeChild(elem);

  elem = document.createElement(elementName);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + 1 + "");
  document.body.removeChild(elem);
}

function testDocumentPropertyWithAttributes(attributeName, elementName, attributes, base) {
  var elem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElementNS(ns, elementName);
  for (let k in attributes)
    elem.setAttribute(k, attributes[k]);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + "");
  document.body.removeChild(elem);

  elem = document.createElement(elementName);
  for (let k in attributes)
    elem.setAttribute(k, attributes[k]);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + 1 + "");
  document.body.removeChild(elem);
}

function testElementProperty(elementName, attributeName, subelementName, base) {
  var subelem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElement(elementName);
  subelem = document.createElementNS(ns, subelementName);
  elem.appendChild(subelem);
  shouldBe("elem." + attributeName + ".length", base + "");
  elem.removeChild(subelem);

  subelem = document.createElement(subelementName);
  elem.appendChild(subelem);
  shouldBe("elem." + attributeName + ".length", base + 1 + "");
  elem.removeChild(subelem);
}

function runTest() {
  if (window.testRunner)
    testRunner.dumpAsText();

  description('Tests that HTMLCollection only properly contains HTML elements');

  var elem;
  select = document.createElement("select");
  elem = document.createElementNS(ns, "option");
  select.appendChild(elem);
  shouldBe("select.options.length", "0");
  shouldBe("select.selectedOptions.length", "0");

  elem = document.createElement("option");
  select.appendChild(elem);
  shouldBe("select.options.length", "1");

  testDocumentProperty("images", "img");
  testDocumentProperty("forms", "form");
  testDocumentPropertyWithAttributes("applets", "object", {type: "application/x-java-applet"});
  testDocumentProperty("embeds", "embed");

  // Note that this is run before the final script element on this page is inserted
  testDocumentProperty("scripts", "script", 3);

  testDocumentPropertyWithAttributes("links", "a", {href: "foo"});
  testDocumentPropertyWithAttributes("links", "area", {href: "foo"});
  testDocumentPropertyWithAttributes("anchors", "a", {name: "foo"});

  testElementProperty("map", "areas", "area");
  testElementProperty("table", "rows", "tr");
  testElementProperty("table", "tBodies", "tbody");
  testElementProperty("tr", "cells", "td");
  testElementProperty("thead", "rows", "tr");
  testElementProperty("tbody", "rows", "tr");
  testElementProperty("tfoot", "rows", "tr");
}
</script>
</head>
<body>
  <script>runTest();</script>
</body>
</html>
